package com.example.last.controller;

import com.example.last.common.Result;
import com.example.last.entity.Params;
import com.example.last.entity.Propagate;
import com.example.last.service.PropagateService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RequestMapping("/propagate")
@RestController
public class PropagateController {
    @Resource
    private PropagateService propagateService;
    
    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        System.out.println("ok?");
        PageInfo<Propagate> info = propagateService.findBySearch(params);
        return Result.success(info);
    }

    //新增信息
    @PostMapping
    public Result save(@RequestBody Propagate propagate){
        if(propagate.getId() == null){
            propagateService.add(propagate);
        }else{
            propagateService.update(propagate);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        propagateService.delete(id);
        return Result.success();
    }

    //批量删除
    @PutMapping("/batch")
    public Result batchdel(@RequestBody List<Propagate> list){
        for(Propagate propagate : list){
            propagateService.delete(propagate.getId());
        }
        return Result.success();
    }

    @GetMapping("/select")
    public Result selectAll(){
        List<Propagate> list = propagateService.findAll();
        return Result.success(list);
    }

    @GetMapping("/selectAll")
    public Result selectAllData(){
        List<Propagate> list = propagateService.findAllData();
        return Result.success(list);
    }

    @GetMapping("/selectById/{id}")
    public Result selectAll(@PathVariable Integer id){
        System.out.println("===========================");
        Propagate propagate = propagateService.findById(id);
        return Result.success(propagate);
    }


}
